package day03;

import java.sql.*;
import java.util.Scanner;

public class Demo05 {
    public static void main(String[] args) throws Exception {
        PreparedStatement QueryStatement = null;
        PreparedStatement InsertStatement = null;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.next();
        System.out.println("请输入密码:");
        String password = scanner.next();

        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from user where username = ?";
        String insertSql = "insert into user(username,password) values(?,?)";
        QueryStatement = connection.prepareStatement(sql);
        QueryStatement.setObject(1, username);
        ResultSet resultSet = QueryStatement.executeQuery();
        if (resultSet.next()) {
            System.out.println("用户已存在");
        } else {
            InsertStatement = connection.prepareStatement(insertSql);
            InsertStatement.setObject(1, username);
            InsertStatement.setObject(2, password);
            InsertStatement.executeUpdate();

        }
        JDBCUtils.close(connection, InsertStatement, resultSet);
        JDBCUtils.close(connection, QueryStatement, null);
        scanner.close();


    }
}
